enyo.kind({
    name: "MyApps.Newsletter",
    kind: enyo.VFlexBox,
    events: {
        onBack: "",
    },
    components: [    /**
     * Service
     */
    {
        name: "getData",
        kind: "WebService",
        url: "http://golfplus.mobile2you.fr/shops.json",
        onSuccess: "gotData",
        onFailure: "gotDataFailure"
    }, {
        name: "registerService",
        kind: "WebService",
        //     method: "PUT",
        onSuccess: "registered",
        onFailure: "registerFailure"
    },    /**
     * Layout
     */
    {
        name: "pane",
        kind: "Pane",
        flex: 1,
        className: "newsletter",
        components: [{
            kind: "VFlexBox",
            flex: 1,
            components: [{
            
                kind: "PageHeader",
                className: "enyo-header-dark",
                components: [{
                    kind: "IconButton",
                    onclick: "doBack",
                    icon: "images/menu-icon-back.png",
                    className: "enyo-button-dark"
                }, {
                    kind: "VFlexBox",
                    flex: 1,
                    align: "center",
                    components: [{
                        content: "Newsletter"
                    }, ]
                }, {
                    name: "registerButton",
                    kind: "Button",
                    caption: "S'INSCRIRE",
                    className: "enyo-button-dark",
                    onclick: "register"
                }, ]
            }, {
                kind: "Scroller",
                flex: 1,
                className: "newslettercontent",
                components: [{
                    content: "Pour vous inscrire à notre newsletter, merci de bien vouloir remplir les champs suivants.",
                    align: "center",
                    className: "newslettertext",
                
                }, {
                    name: "name",
                    kind: "Input",
                    hint: "Prénom",
                    alwaysLooksFocused: "true",
                    className: "newsletterfield",
                }, {
                    name: "surname",
                    kind: "Input",
                    hint: "Nom",
                    alwaysLooksFocused: "true",
                    className: "newsletterfield",
                }, {
                    name: "email",
                    kind: "Input",
                    hint: "Email",
                    alwaysLooksFocused: "true",
                    className: "newsletterfield",
                    inputType: "email"
                }, {
                    name: "postcode",
                    kind: "Input",
                    hint: "Code Postal",
                    alwaysLooksFocused: "true",
                    className: "newsletterfield",
                }, {
                    kind: "VFlexBox",
                    align: "center",
                    components: [{
                        content: "Mon magasin le plus proche",
                    }, ]
                }, {
                    name: "storeButton",
                    kind: "Button",
                    onclick: "showDialog",
                    caption: "www.golfplus.fr",
                    className: "newsletterfield",
                }, {
                    name: "storesDialog",
                    kind: "ModalDialog",
                    components: [{
                        kind: "Scroller",
                        height: "300px",
                        components: [{
                            flex: 1,
                            kind: "Item",
                            content: "www.golfplus.fr",
                            onclick: "listItemGolfplusFrClick"
                        }, {
                            name: "list",
                            kind: "VirtualRepeater",
                            flex: 1,
                            onSetupRow: "listSetupRow",
                            components: [{
                                name: "itemLabel",
                                kind: "Item",
                                flex: 1,
                                onclick: "listItemClick"
                            }, ]
                        }, ]
                    }, ]
                }, {
                    kind: "ModalDialog",
                    name: "registrationInfo",
                    caption: "",
                    components: [{
                        name: "infoContent",
                        content: "",
                    }, {
                        kind: "Button",
                        caption: "OK",
                        onclick: "closePopup",
                        style: "margin-top:10px"
                    }, ]
                }]
            }, ],
        }, ]
    }, ],
    
    
    
    
    create: function(){
        this.inherited(arguments);
        this.data = [];
        this.selectedstore = "www.golfplus.fr";
        this.$.getData.call();
    },
    
    register: function(){
        if (!this.isEmailValid()) {
            this.$.registrationInfo.openAtCenter();
            this.$.infoContent.setContent("E-mail n'est pas valide.");
        }
        else {
            var name = this.$.name.getValue();
            var surname = this.$.surname.getValue();
            var email = this.$.email.getValue();
            var postcode = this.$.postcode.getValue();
            var store = this.selectedstore;
            
            this.$.registerService.setUrl("http://golfplus.mobile2you.fr/newsletters/register/" + email + "?prenom=" + name + "&nom=" + surname + "&cp=" + postcode + "&magasin=" + store);
            console.log(this.$.registerService.getUrl());
            this.$.registerService.call();
            this.registered();
        }
    },
    registered: function(){
        console.log("registered");
        this.cleanFormular();
        
        this.$.registrationInfo.openAtCenter();
        this.$.infoContent.setContent("Inscription réussie. Vous avez été correctement inscrit à la newsletter.");
    },
    registerFailure: function(inSender, inResponse){
        console.log("registerFailure");
        this.$.registrationInfo.openAtCenter();
        this.$.infoContent.setContent("Echec de l'enregistrement. Cet e-mail est déjà enregistré.");
    },
    closePopup: function(inSender, inEvent){
        this.$.registrationInfo.close();
    },
    
    
    cleanFormular: function(){
        this.$.name.setValue(null);
        this.$.surname.setValue(null);
        this.$.email.setValue(null);
        this.$.postcode.setValue(null);
    },
    
    
    showDialog: function(){
        this.$.storesDialog.openAtCenter();
    },
    
    closeDialog: function(){
        this.$.storesDialog.close();
    },
    
    listItemGolfplusFrClick: function(inSender, inEvent){
        this.$.storeButton.setCaption(inSender.content);
        this.selectedstore = "www.golfplus.fr";
        this.closeDialog();
    },
    
    listItemClick: function(inSender, inEvent){
        var store = this.data[inEvent.rowIndex];
        this.$.storeButton.setCaption(store.shop.nom);
        this.selectedstore = store.shop.nom;
        this.closeDialog();
    },
    
    goBack: function(inSender, inEvent){
        this.$.pane.back(inEvent);
    },
    
    gotData: function(inSender, inResponse){
        this.data = inResponse;
        console.log("got Data");
        this.$.pane.render();
        this.$.storesDialog.render();
    },
    gotDataFailure: function(inSender, inResponse){
        console.log("got failure from getData");
    },
    listSetupRow: function(inSender, inRow){
        var f = this.data[inRow];
        if (f) {
            this.$.itemLabel.setContent(f.shop.nom);
            return true;
        }
    },
    
    
    isEmailValid: function(){
        var email = this.$.email.getValue();
        var atpos = email.indexOf("@");
        var dotpos = email.lastIndexOf(".");
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
            return false;
        }
        return true;
    }
    
});



